package com.xz.freshmall.taomi.dialog;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.manor.common.CommonAppConfig;
import com.manor.common.HtmlConfig;
import com.manor.common.dialog.AbsDialogFragment;
import com.manor.common.utils.DpUtil;
import com.xz.freshmall.R;

import org.jetbrains.annotations.NotNull;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class PrivacyAgreementDialogFragment extends AbsDialogFragment {

    public static final String SP_IS_FIRST_ENTER_APP = "is_first";

    private TextView tvContent;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_privacy_agreement;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.dialog_sd;
    }

    @Override
    protected boolean canCancel() {
        return true;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = getResources().getDisplayMetrics().widthPixels - DpUtil.dp2px(30);
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(attributes);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        tvContent = (TextView) findViewById(R.id.tv_content);
        String content = "感谢您选择 淘米生活  !我们非常重视您的个人信息和隐私保护,为了更好地保障您的个" +
                "人权益，在您使用我们的产品前，请务必审慎阅读《用户协议》和《隐私政策》内的所有条款，尤其是:\n1.我们对您的个人信息的收集/保存/使用/对外提供/保护等规则条款，以及您的用户权利等条款;" +
                "\n 2. 约定我们的限制责任、免责条款; \n3.其他以颜色或加粗进行标识的重要条款。\n如您对以上协议有任何疑问，" +
                "可通过人工客服或发邮件至3487585187@qq.com与我们联系。" +
                "您点击\"同意并继续\"的行为即表示您已阅读完毕并同意以上协议的全部内容。如您同意以上协议内容，" +
                "请点击\"同意并继续”，开始使用我们的产品和服务!\";";
        tvContent.setText(content);
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(content);
        final int start = content.indexOf("《");//第一个出现的位置
        ssb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NotNull View widget) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");


                Uri content_url = Uri.parse(CommonAppConfig.SERVER_HTML + HtmlConfig.LINK_REGISTER);
                intent.setData(content_url);
                startActivity(intent);
            }

            @Override

            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(mContext.getResources().getColor(R.color.blue));       //设置文件颜色
                // 去掉下划线
                ds.setUnderlineText(false);
            }
        }, start, start + 6, 0);


        int end = content.lastIndexOf("《");
        ssb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(CommonAppConfig.SERVER_HTML + HtmlConfig.LINK_PRIVATE);
                intent.setData(content_url);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(mContext.getResources().getColor(R.color.blue));
                ds.setUnderlineText(false);
            }
        }, end, end + 6, 0);


        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
        tvContent.setText(ssb, TextView.BufferType.SPANNABLE);

        findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
                SPUtils.getInstance().put(SP_IS_FIRST_ENTER_APP, false);
                dialog.cancel();
            }
        });

        findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onNextListener();
                }
                SPUtils.getInstance().put(SP_IS_FIRST_ENTER_APP, true);
                dialog.cancel();
            }
        });

        if (getDialog() != null) {
            getDialog().setCanceledOnTouchOutside(false);
        }
        setCancelable(false);

    }

    private OnNextListener listener;

    public void setOnNextListener(OnNextListener listener) {
        this.listener = listener;
    }

    public interface OnNextListener {
        void onNextListener();
    }


}
